<?php
namespace app\index\controller;
use app\index\model\Proxy;
use app\index\service\ImageService;
use phpsdk\AipOcr;
use Jwt;
use app\user\model\Admin as AdminModel;
use app\server\model\Organization as OrganizationModel;
use app\user\model\User as UserModel;
use think\Controller;
use think\facade\Session;
use think\Queue;
use app\index\service\WeChatService;
use think\facade\Cache;

class Index extends Controller
{
    //index 方法
    public function index()
    {
        $data =$this->request->get();
        if($data){
        $signature = $data['signature'];
        $timestamp = $data['timestamp'];
        $nonce = $data['nonce'];
        $echostr = $data['echostr'];
        //计算微信签名
        $token ='func';
        //将参数组成一维数组
        $signeSeed = [$token, $timestamp, $nonce];
        //对参数字典序排序
        sort($signeSeed, SORT_STRING);
        //拼接成字符串
        $signeStr = implode($signeSeed);
        //加密字符串成签名
        $signeHash = sha1($signeStr);

        if ($signeHash == $signature) {
            echo $echostr;
            }else{
                return false;
            }
        }
    }
    //getToken
    public function getToken(){
        $data = $this->request->param();
        $w =new WeChatService($data);
        $data= $w->getToken($data['app_id']);
        return json_encode($data);
    }
    //获取所有用户
    public function getUsers(){
        $data = $this->request->param();
        $w =new WeChatService($data);
        $d= [];
        $w->getUserOpenId('',$d);
        return json_encode($d);
    }
    //测试cache
    public function getCache(){
        $token = Cache::get('token');
        if(!$token){
            $a =111111 ;
            $expire=30;
            Cache::set('token',$a,$expire);
            $token = Cache::get('token');
            $token =json_encode($token);
            dump('过期了'.$token);
        }else{
            $token =json_encode($token);
            dump('未过期'.$token);
        }
        return $token;
    }

    public function pushlc(){
        $data = $this->request->param();
        $w =new WeChatService($data);
        $d = [];
        $d = $w->getUserOpenId('',$d);
        foreach($d as $value){
            $a=array('openid'=> $value,'name'=>$data['name'],'time'=>$data['time'],'url'=>$data['url'],'type'=>$data['type'],'address'=>$data['unit'],'context'=>$data['context'],'remark'=>$data['remark']);
            $queueName = 'testQueue1';
            $isPushed =false;
            $isPushed = Queue::push(TestQueue::class, $a,$queueName);
            unset($a);
            if ($isPushed !=false) {
                dump('用户['.$value.']'." 加入.'$queueName'.成功");
                unset($isPushed);
            } else {
                dump("队列加入失败");
                unset($isPushed);
            }
        }
    }
    //分别发送多个队列
    public function pushlc2(){
        $data = $this->request->param();
        $w =new WeChatService($data);
        $count = 0;
        $w->getUserOpenId2('',$count,$data);
    }
    //一次发送
    public function pushlcone(){
        $data = $this->request->param();
        $w =new WeChatService($data);
        // $queueName = 'testQueue1';
        $res = $w->pushlcG2($data);
        // $res = Queue::push(TestQueue::class, $data,$queueName);
        dump($res);
    }
    
    //获取消息模版列表
    public function gettemplate()
    {
        $data = $this->request->param();
        $w =new WeChatService($data);
        $arr = $w->gettemplate();
        return json_encode($arr);
    }
    //协议页面
    public function proxy()
    {
        return $this ->fetch('proxy');
    }
    //版本信息
    public function right()
    {
        return $this ->fetch('proxy');
    }
    //下载页面
    public function download()
    {
        return $this ->fetch('proxy');
    }

    //获取admin登录token
    public function getAdminToken(){
        $data = $this->request->param();
        $res = new AdminModel();
        if(empty($data) ){
            return 'fail,未定义参数tel';
        }else{
            $map =['tel'=>$data['tel'],'password'=>$data['password']];
            $u= $res->get($map);
            $jwt =new Jwt;
            $token = $jwt->getToken($u['id']);
            Session::set('token',$token);
            return json(['code'=>200,'token'=>$token]);
        }
    }
    //获取机构登录的的登录token
    public function getorgaToken(){
        $data = $this->request->param();
        $res = new OrganizationModel();
        if(empty($data) ){
            return 'fail,未定义参数tel';
        }else{
            $map =['tel'=>$data['tel'],'password'=>$data['password']];
            $u= $res->get($map);
            $jwt =new Jwt;
            $token = $jwt->getToken($u['o_id']);
            Session::set('token',$token);
            return json(['code'=>200,'token'=>$token]);
        }
    }
    //获取用户的登录token
    public function getuserToken(){
        $data=$this->request->param();//保存获取用户提过来的数据
        $user=new UserModel();
        if(empty($data) ){
            return 0;
        }else{
            $map =['tel'=>$data['tel']];
            $u =  $user->get($map);
            $jwt =new Jwt;
            $token = $jwt->getToken($u['u_id']);
            Session::set('token',$token);
            return json(['code'=>200,'token'=>$token]);
        }
    }
    /**
     * 获取协议
     * */
    public function getProxy()
    {
        $data = $this->request->param();
        $res=new Proxy();
        return $res->selectModel($data);
    }
    /*上传Banner图片*/
    public function upload()
    {
        $url = "static/sharehis/";
        $image = new ImageService();
        return $image->upload($url);
    }
    //身份证识别算法，调用百度api
    public function image_dist()
    {
        $data = $this->request->param();
        $image = file_get_contents($data['img']);
        $client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        $data=$client->idcard($image, $data['side']);
        return json_encode($data);
    }
    // //图像裁剪算法，调用百度api
    // public function cut(){
    //     $data = $this->request->param();
    //     $image = file_get_contents($data['img']);
    //     $client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
    //     // 调用图像主体检测
    //     $r=  $client->objectDetect($image);
    //     if($r){
    //         $re = $r['result'];
    //         $img = Image::open($data['img']);
    //         $a = $img->crop($re['width'], $re['height'],$re['left'],$re['top'])->save('static/sharehis/crop.png');
    //         return $a;
    //     }
    // }

}
